using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Autofac;
using Catastrophe.Common.ObjM;

namespace Catastrophe.Initialize.IOCProvier
{
	public static class ContainerBuilderEx
	{
		public static void RegisterAllClass<T>(this ContainerBuilder container, Assembly assembly = null)
		{
			if (assembly == null)
			{
				assembly = Assembly.GetCallingAssembly();
			}
			Dictionary<string, List<Type>> assemblyTypes = ReflectionUtil.GetAssemblyTypes<T>(0, new Assembly[1] { assembly });
			foreach (Type item in assemblyTypes.First().Value)
			{
				IOCRegisterDescribe iOCRegisterDescribe = Attribute.GetCustomAttribute(item, typeof(IOCRegisterDescribe)) as IOCRegisterDescribe;
				if (iOCRegisterDescribe != null)
				{
					string serviceName = (string.IsNullOrWhiteSpace(iOCRegisterDescribe.Name) ? item.FullName : iOCRegisterDescribe.Name);
					container.RegisterType(item).As<T>().Named<T>(serviceName);
				}
			}
		}

		public static void RegisterAllInterface<T>(this ContainerBuilder container, Assembly assembly = null)
		{
			if (assembly == null)
			{
				assembly = Assembly.GetCallingAssembly();
			}
			Dictionary<string, List<Type>> assemblyTypes = ReflectionUtil.GetAssemblyTypes<T>(1, new Assembly[1] { assembly });
			foreach (Type item in assemblyTypes.First().Value)
			{
				IOCRegisterDescribe iOCRegisterDescribe = Attribute.GetCustomAttribute(item, typeof(IOCRegisterDescribe)) as IOCRegisterDescribe;
				if (iOCRegisterDescribe != null)
				{
					string serviceName = (string.IsNullOrWhiteSpace(iOCRegisterDescribe.Name) ? item.FullName : iOCRegisterDescribe.Name);
					container.RegisterType(item).As<T>().Named<T>(serviceName);
				}
			}
		}
	}
}
